﻿// --------------------------------
// <copyright file="MainForm.cs" company="TRONSoft">
//     Copyright (c) TRONSoft.
// </copyright>
// <license>
//   This source code is subject to terms and conditions of the Microsoft
//   Public License. A copy of the license can be found in the License.html
//   file at the root of this distribution. If you cannot locate the
//   Microsoft Public License, please send an email to dlr@microsoft.com.
//   By using this source code in any fashion, you are agreeing to be bound 
//   by the terms of the Microsoft Public License. You must not remove this
//   notice, or any other, from this software.
// </license>
// <author>Tron</author>
// ---------------------------------

using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace Actucal
{
    public partial class MainForm : Form
    {
        //private ContentForm contentForm = new ContentForm();
        private readonly AddCalculationForm addCalculationForm = new AddCalculationForm();
        private readonly AddConstantForm addConstantForm = new AddConstantForm();
        private readonly CalculationEntryForm calculationEntryForm = new CalculationEntryForm();
        private readonly ConstantEntryForm constantEntryForm = new ConstantEntryForm();
        private readonly DataEntryForm productEntryForm = new DataEntryForm();

        public MainForm()
        {
            InitializeComponent();

            //contentForm.Show(dockPanel, DockState.Document);
            constantEntryForm.Show(dockPanel, DockState.DockLeft);
            calculationEntryForm.Show(dockPanel, DockState.DockLeft);
            productEntryForm.Show(dockPanel, DockState.DockLeft);
            addCalculationForm.Show(dockPanel, DockState.Document);
            addConstantForm.Show(dockPanel, DockState.Document);

            productEntryForm.CurrentProductChanged += productEntryForm_CurrentProductChanged;
            calculationEntryForm.CurrentCalculationChanged += calculationEntryForm_CurrentCalculationChanged;
            constantEntryForm.CurrentConstantChanged += constantEntryForm_CurrentConstantChanged;
        }

        private void constantEntryForm_CurrentConstantChanged(object sender, ArgumentEventArgs<string> e)
        {
            if (null == ProductsService.CurrentProduct)
                return;

            addConstantForm.NameOfConstant = e.Argument;
            addConstantForm.ValueOfConstant = ProductsService.CurrentProduct.Constants[e.Argument].Value;
        }

        private void calculationEntryForm_CurrentCalculationChanged(object sender, ArgumentEventArgs<string> e)
        {
            if (null == ProductsService.CurrentProduct)
                return;

            addCalculationForm.CalculationName = e.Argument;
            addCalculationForm.CalculationCode = ProductsService.CurrentProduct.FindAttribute(e.Argument);
        }

        private void productEntryForm_CurrentProductChanged(object sender, EventArgs e)
        {
            Text = string.Format("Actucal, huidig product: {0}", ProductsService.CurrentProduct.Name);
        }

        private void miGenerateCalculationEngine_Click(object sender, EventArgs e)
        {
            //using (GenerateEngineForm frm = new GenerateEngineForm(/* contentForm.Products */ ProductsService.Products))
            //{
            //    frm.ShowDialog(this);
            //}
        }

        private void dockPanel_ActiveContentChanged(object sender, EventArgs e)
        {
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Tools.SaveDataAsBinary(Path.ChangeExtension(Assembly.GetEntryAssembly().Location, "bin"),
                                   ProductsService.Products);
        }

        private void dockPanel_ActiveDocumentChanged(object sender, EventArgs e)
        {
            if (dockPanel.ActiveDocument != null)
                dockPanel.ActiveDocument.DockHandler.Form.Enabled = (ProductsService.CurrentProduct != null);
        }

        private void dockPanel_ActivePaneChanged(object sender, EventArgs e)
        {
        }

        private void dockPanel_TabIndexChanged(object sender, EventArgs e)
        {
        }

        private void miCalculationModule_Click(object sender, EventArgs e)
        {
            using (GenerateCalculationModuleForm frm = new GenerateCalculationModuleForm())
            {
                frm.ShowDialog(this);
            }
        }
    }
}